package com.qymerge.manage.core.utils;

import com.qymerge.manage.base.dto.OrganizationalStructureDTO;
import com.qymerge.manage.system.dto.SysMenuDTO;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Desc
 * User cong.li
 * Date 2017/4/5
 * Time 11:00
 */
public class OrganizationalStructureUtils {

    public static String formatMenuJson(List<OrganizationalStructureDTO> menuDtoList) {
        List<OrganizationalStructureDTO> treeList = new ArrayList<OrganizationalStructureDTO>();
        List<Long> idList = new ArrayList<Long>();
        if (!CollectionUtils.isEmpty(menuDtoList)) {
            for (int i = 0; i < menuDtoList.size(); i++) {
                OrganizationalStructureDTO dto = menuDtoList.get(i);
                idList.add(dto.getId());
            }
            for (int j = 0; j < menuDtoList.size(); j++) {
                OrganizationalStructureDTO dto = menuDtoList.get(j);
                if (!idList.contains(dto.getParent_id())) {
                    dto.setChildren(getRecursionChildList(menuDtoList, dto.getId()));
                    treeList.add(dto);
                }
            }
        }
        Collections.sort(treeList);
        String json = GsonUtil.t2Json2(treeList);
        return json;
    }

    public static List getRecursionChildList(List<OrganizationalStructureDTO> dataList, long parentId) {
        List<OrganizationalStructureDTO> childList = new ArrayList<OrganizationalStructureDTO>();
        if (!CollectionUtils.isEmpty(dataList)) {
            for (int i = 0; i < dataList.size(); i++) {
                OrganizationalStructureDTO dto = dataList.get(i);
                if (String.valueOf(parentId).equals(String.valueOf(dto.getParent_id()))) {
                    dto.setChildren(getRecursionChildList(dataList, dto.getId()));
                    childList.add(dto);
                }
            }
        }
        Collections.sort(childList);
        return childList;
    }


}
